clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* get market return
import sas using $Data/CRSP/mcti.sas7bdat, case(lower) clear
rename caldt date
rename t90ret rf

gen datem=mofd(date)
format datem %tm
format date  %td
keep datem date rf 

tempfile rf
save "`rf'", replace
* ***********************************************************************************************
* get market return
import sas using $Data/CRSP/msi.sas7bdat, case(lower) clear
format date  %td
rename ewretd mkt
keep date mkt 

gen datem=mofd(date)
format datem %tm

merge 1:1 datem using "`rf'"
keep if _merge==3
drop _merge

tsset datem
forvalues x=1(1)5 {
	gen  Mkt`x'Yr=1
	gen   rf`x'Yr=1
} 
forvalues x=1(1)12 {
	replace  Mkt1Yr=Mkt1Yr*(1+ F`x'.mkt)
	replace   rf1Yr= rf1Yr*(1+ F`x'.rf )
}
forvalues x=13(1)24 {
	replace  Mkt2Yr=Mkt2Yr*(1+ F`x'.mkt)
	replace   rf2Yr= rf2Yr*(1+ F`x'.rf )
}
forvalues x=25(1)36 {
	replace  Mkt3Yr=Mkt3Yr*(1+ F`x'.mkt)
	replace   rf3Yr= rf3Yr*(1+ F`x'.rf )
}
forvalues x=37(1)48 {
	replace  Mkt4Yr=Mkt4Yr*(1+ F`x'.mkt)
	replace   rf4Yr= rf4Yr*(1+ F`x'.rf )
}
forvalues x=49(1)60 {
	replace  Mkt5Yr=Mkt5Yr*(1+ F`x'.mkt)
	replace   rf5Yr= rf5Yr*(1+ F`x'.rf )
}
gen Mkt1Mo=(1+F1.mkt)
gen  rf1Mo=(1+F1.rf )
gen Mkt1Qr=(1+F1.mkt)*(1+F2.mkt)*(1+F3.mkt)
gen  rf1Qr=(1+F1.rf )*(1+F2.rf )*(1+F3.rf )

keep datem Mkt*Yr Mkt*Qr Mkt*Mo rf*Yr rf*Qr rf*Mo
merge 1:1 datem using "`rf'"
keep if _merge==3
drop _merge
order datem *Mo *Qr *Yr
* *****************************************************************
save $Work/mktrf, replace

!find $Temp -type f -delete
